package library.and.xps.com.step.m.model;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用特殊处理的单例拼接请求参数 并生成json对象
 */
public class RequestParams {
    private String userid;
    private String ico;//头像地址
    private String nickname;
    private String password;
    private String sex;
    private String birth;
    private String weight;
    private String stature;
    private String email;
    private String phone;
    private String team;
    private String address;
    private String profession;//用户职位
    private String autograph;//用户个性签名
    private String verificationcode;
    private String state;//用户状态
    private String token;
    private String tname;
    private String taname;
    private String teamid;
    private String page;
    private String creater;
    private String type;
    private String description;
    private String jpushid;
    private String mark;//头像 maybe
    private String enterprise;
    private String departmentid;//部门ID
    private String intname;//在团队中的姓名
    private String dname;//部门名称
    private String parentid;
    private String format;
    private String describe;
    private String endtime;
    private String starttime;
    private String exercise;
    private String goal;

    private String challenger;
    private String bechallenged;
    private String chip;
    private String selfchip;

    private String readingtimes;
    private String pkids;

    private String isno ;
    private String scale;

    public RequestParams setReadingtimes(String readingtimes) {
        this.readingtimes = readingtimes;
        return this;
    }

    public RequestParams setJpushid(String jpushid) {
        this.jpushid = jpushid;
        return this;
    }

    public JSONObject getJson() {
        StringBuilder sb = new StringBuilder();
        JSONObject json = new JSONObject();
        String jsonValue;

        sb.append("{");
        for (Field field : getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(this);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if ("ourInstance".equals(field.getName() + "")
                    || "CommonRequest".equals(field.getName() + "")
                    || "serialVersionUID".equals(field.getName() + ""))
                continue;

            /**
             * 因为pkids为数组 所以这样写
             * {"pkids":[1],"readingtimes":"2147483647","userid":"2"}
             * 需要改进?
             * */
            if (value != null && !TextUtils.isEmpty(value + "") && !"NULL".equals(value + "")){
                    if(field.getName().equals("pkids")){
                        sb.append("\"" + field.getName() + "\"" + ":"   + "[" + value + "]" + ",");
                        continue;
                    }
            }

            if (value != null && !TextUtils.isEmpty(value + "") && !"NULL".equals(value + ""))
                sb.append("\"" + field.getName() + "\"" + ":" + "\"" + value + "\"" + ",");

        }

        jsonValue = sb.toString();
        jsonValue = jsonValue.substring(0, jsonValue.length() - 1) + "}";
        try {
            json = new JSONObject(jsonValue);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("RequestParams", "创建JSON异常");
            return json;
        }

        return json;
    }


    public static RequestParams getNewInstance() {
        return new RequestParams();
    }

    private RequestParams() {
        try {
            Reflect();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public void Reflect() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Field[] field = getClass().getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
            String name = field[i].getName();
            name = name.substring(0, 1).toUpperCase() + name.substring(1);
            String type = field[i].getGenericType().toString();
            if (type.equals("class java.lang.String"))
                getClass().getDeclaredMethod("set" + name, String.class).invoke(this, "NULL");
        }
    }

    public RequestParams setIsno(String isno) {
        this.isno = isno;
        return this;
    }

    public RequestParams setScale(String scale) {
        this.scale = scale;
        return this;
    }

    public RequestParams setSelfchip(String selfchip) {
        this.selfchip = selfchip;
        return this;
    }

    public RequestParams setChallenger(String challenger) {
        this.challenger = challenger;
        return this;
    }

    public RequestParams setBechallenged(String bechallenged) {
        this.bechallenged = bechallenged;
        return this;
    }

    public RequestParams setChip(String chip) {
        this.chip = chip;
        return this;
    }

    public RequestParams setPkids(String pkids) {
        this.pkids = pkids;
        return this;
    }

    public RequestParams setGoal(String goal) {
        this.goal = goal;
        return this;
    }

    public RequestParams setPage(String page) {
        this.page = page;
        return this;
    }


    public RequestParams setDname(String dname) {
        this.dname = dname;
        return this;
    }


    public RequestParams setParentid(String parentid) {
        this.parentid = parentid;
        return this;
    }

    public RequestParams setTaname(String taname) {
        this.taname = taname;
        return this;
    }

    public RequestParams setFormat(String format) {
        this.format = format;
        return this;
    }

    public RequestParams setDescribe(String describe) {
        this.describe = describe;
        return this;
    }

    public RequestParams setEndtime(String endtime) {
        this.endtime = endtime;
        return this;
    }

    public RequestParams setStarttime(String starttime) {
        this.starttime = starttime;
        return this;
    }

    public RequestParams setExercise(String exercise) {
        this.exercise = exercise;
        return this;
    }

    public String getUserid() {
        return userid;
    }

    public RequestParams setUserid(String userid) {
        this.userid = userid;
        return this;
    }

    public String getEnterprise() {
        return enterprise;
    }

    public RequestParams setEnterprise(String enterprise) {
        this.enterprise = enterprise;
        return this;
    }

    public String getDepartmentid() {
        return departmentid;
    }

    public RequestParams setIntname(String intname) {
        this.intname = intname;
        return this;
    }

    public RequestParams setDepartmentid(String departmentid) {
        this.departmentid = departmentid;
        return this;
    }


    public RequestParams setIco(String ico) {
        this.ico = ico;
        return this;
    }

    public RequestParams setTname(String tname) {
        this.tname = tname;
        return this;
    }

    public RequestParams setCreater(String creater) {
        this.creater = creater;
        return this;
    }

    public RequestParams setType(String type) {
        this.type = type;
        return this;
    }

    public RequestParams setDescription(String description) {
        this.description = description;
        return this;
    }

    public RequestParams setMark(String mark) {
        this.mark = mark;
        return this;
    }


    public RequestParams setNickname(String nickname) {
        this.nickname = nickname;
        return this;
    }


    public RequestParams setPassword(String password) {
        this.password = password;
        return this;
    }

    public String getTeamid() {
        return teamid;
    }

    public RequestParams setTeamid(String teamid) {
        this.teamid = teamid;
        return this;
    }


    public RequestParams setSex(String sex) {
        this.sex = sex;
        return this;
    }

    public String getTname() {
        return tname;
    }

    public String getCreater() {
        return creater;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public String getMark() {
        return mark;
    }

    public RequestParams setBirth(String birth) {
        this.birth = birth;
        return this;
    }

    public RequestParams setWeight(String weight) {
        this.weight = weight;
        return this;
    }


    public RequestParams setStature(String stature) {
        this.stature = stature;
        return this;
    }


    public RequestParams setEmail(String email) {
        this.email = email;
        return this;
    }


    public RequestParams setPhone(String phone) {
        this.phone = phone;
        return this;
    }

    public String getTeam() {
        return team;
    }

    public RequestParams setTeam(String team) {
        this.team = team;
        return this;
    }


    public RequestParams setAddress(String address) {
        this.address = address;
        return this;
    }


    public RequestParams setProfession(String profession) {
        this.profession = profession;
        return this;
    }


    public RequestParams setAutograph(String autograph) {
        this.autograph = autograph;
        return this;
    }


    public RequestParams setVerificationcode(String verificationcode) {
        this.verificationcode = verificationcode;
        return this;
    }


    public RequestParams setState(String state) {
        this.state = state;
        return this;
    }


    public RequestParams setToken(String token) {
        this.token = token;
        return this;
    }

    @Override
    public String toString() {
        return "CommonRequest{" +
                "userid='" + userid + '\'' +
                ", ico='" + ico + '\'' +
                ", nickname='" + nickname + '\'' +
                ", password='" + password + '\'' +
                ", sex='" + sex + '\'' +
                ", birth='" + birth + '\'' +
                ", weight='" + weight + '\'' +
                ", stature='" + stature + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", team='" + team + '\'' +
                ", address='" + address + '\'' +
                ", profession='" + profession + '\'' +
                ", autograph='" + autograph + '\'' +
                ", verificationcode='" + verificationcode + '\'' +
                ", state='" + state + '\'' +
                ", token='" + token + '\'' +
                '}';
    }
}
